#include <cmath>

#include "VerticalBouncing.h"
#include "geom.h"
#include "TexturedEntity.h"

VerticalBouncing::VerticalBouncing(TexturedEntity* entity, const float y0, const float yMax, const float factor)
: Behaviour(entity), m_y0(y0), m_yMax(yMax), m_elapsedTime(0.0f), m_factor(factor)
{
    m_Vy = sqrt(2.0f * GRAVITY * (yMax - m_y0));
}

VerticalBouncing::~VerticalBouncing(void)
{
}

void VerticalBouncing::prepare(float dt)
{
    // Calculate new position
    m_elapsedTime += (m_factor * dt);   
    float pos = m_y0 + (m_Vy * m_elapsedTime) - (0.5f * GRAVITY * m_elapsedTime * m_elapsedTime);
    if (pos < m_y0)
    {
        m_elapsedTime = 0.0f;
        pos = m_y0;
    }

    // Set the entity's position
    Vector3 currentPosition = m_entity->getPosition();
    m_entity->setPosition(Vector3(currentPosition.x, pos, currentPosition.z));
}
